關於 物件 Object - 取值、新增、刪除


Posted by hoyi-23 on 2021-06-21

var temp={
    name : 'Andy';
    age : 18 ;
    family: {
        mom: 'Betty';
        dad: 'Jake';
    }
}

物件取值

方法一: 使用 ' 點 . '

//變數名稱.屬性
console.log(temp.name); //Andy

console.log(temp.family.dad); //Jake

方法二: 使用 ' 中括號 []'

//變數名稱[屬性]
//變數名稱[變數]
//不但可以用屬性取值 也可以用變數
console.log(temp['name']); //Andy

var a = 'name';
console.log(temp[a]); //Andy

屬性名稱永遠是字串

物件新增值

方法一: 直接使用 '點 . '

temp.birthday = 'July 10th';
console.log(temp['birthday']) ; // 'July 10th'

方法一: 直接使用 ' 中括號 [] '

temp['birthday'] = 'July 10th';
console.log(temp['birthday']) ; // 'July 10th'

物件刪除值

方法一: 使用delete

delete temp.birthday;
delete temp['age'];

#物件取值 #物件新增 #物件 #物件刪除







Related Posts

PyTorch 踩坑紀錄:one of the variables needed for gradient computation has been modified by an inplace operation

PyTorch 踩坑紀錄:one of the variables needed for gradient computation has been modified by an inplace operation

在 Express 上面把資料變美吧

在 Express 上面把資料變美吧

【JS基礎】字串處理

【JS基礎】字串處理


Comments